package com;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.yuhang.demo.util.PostUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author yuhang
 * @desc
 * @since 2022/7/26
 */
public class TaobaoApiTest {

    private static String api = "http://fs.ximgs.net/";

    public static void main(String[] args) throws Exception {

//        String ret = goodsList();

//        String ret=distributionLogs();
//        String ret =productDetail();

//        String ret=getCategory();

//        String ret=productByPlatform();
        String ret =operatorPddService();
        System.out.println(ret);
    }



    private static String operatorPddService() throws Exception {
        String url = "http://thirdapi.ximgs.net/api/op/go2/operatorPddService";
        Map<String,Object> p =new HashMap<>();
        p.put("interName","getPddSeller");
        p.put("data","{\"userId\":881085,\"shopId\":\"367682320\"}");
      return   httpCloudJSONxx(url,p);
    }

    private static String productByPlatform() throws Exception {
        String url = api + "/api/goods/productByPlatform";
        Map<String, Object> map = new HashMap<>();
        map.put("pn","1");
        map.put("ps","20");
//        map.put("keyword","陈丽华");
        map.put("platform","e3e3");

        Map<String,Object> p =new HashMap<>();
        p.put("data",map);
        return httpCloudJSON(url, p, null);
    }
    private static String getCategory() throws Exception {
        String url = api + "/api/goods/getCategory";
        Map<String, Object> map = new HashMap<>();
        map.put("id", 3062339);
        map.put("taobao_user_id","2610162904");
        return httpCloudJSON(url, map, null);
    }
    private static String productDetail() throws Exception {
        String url = api + "/api/goods/productDetail";

        Map<String,Object> p = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", 3062339);
        p.put("data",map);
        return httpCloudJSON(url, p, null);
    }

    private static String distributionLogs() throws Exception {
        String url = api + "/api/goods/distributionLogs";
        Map<String, Object> map = new HashMap<>();
        return httpCloudJSON(url, map, null);
    }

    private static String goodsList() throws Exception {
        String url = api + "/api/goods/list";
        Map<String, Object> map = new HashMap<>();
//        map.put("categoryId", 1);
//        map.put("taobao_user_id", 1);
        return httpCloudJSON(url, map, null);
    }


    public static String httpCloudJSON(String url, Map<String, Object> params, String headerToken) throws Exception {

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
//        if (StringUtils.isNotBlank(headerToken)) {
//            post.setHeader("Authorization", headerToken);
//        }
//        post.setHeader("user-source", "app");
        StringEntity stringEntity = new StringEntity(JSON.toJSONString(params));
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        post.setEntity(stringEntity);
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                return null;
            }
        });
        return response;
    }



    public static String httpCloudJSONxx(String url, Map<String, Object> params) throws Exception {
        String secret = "go2CN|20220119wearethebest";
        String nonce = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis();
        String key = SecureUtil.sha1(secret + nonce + timestamp);
        HttpPost post = new HttpPost(url);
        post.setHeader("sign",key);
        post.setHeader("nonce", nonce);
        post.setHeader("timestamp", timestamp+"");
        CloseableHttpClient client = HttpClients.createDefault();

        String mds = MD5.md5Encode(timestamp+"8c171fb0029da1f3a4fb21b1be3650ff");

        String str=" curl -X POST -d '%s' -H 'Content-type:application/json' -H '%s' -H '%s' -H '%s' -H '%s' -H '%s'  'http://gw-api.pinduoduo.com/custom/router/f28c2d01cc83009dfef04c6fd907a8cf'";
        System.out.println(String.format(str,JSON.toJSONString(params),"sign:"+key,"nonce:"+nonce,"timestamp:"+timestamp,"X-PDD-SG-TIMESTAMP:"+timestamp,"X-PDD-SG-SIGN:"+mds));


        StringEntity stringEntity = new StringEntity(JSON.toJSONString(params));
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        post.setEntity(stringEntity);
        String response = client.execute(post, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity, "utf-8") : null;
                } else {
                    System.out.println(response);
                }
                System.out.println(status);
                return null;
            }
        });
        return response;
    }


}
